package com.tz.online.book.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


import com.tz.online.book.dao.IBookDao;
import com.tz.online.entity.Book;
import com.tz.online.entity.Category;
import com.tz.online.util.JdbcUtils;

public class BookDaoImpl implements IBookDao {

	@Override
	public List<Book> selectBookByCondition(int now, int size, String cate) {
		
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		
		Book book = null;
		List<Book> books = new ArrayList<Book>();
		
		try {
			conn = JdbcUtils.getConnection();
			st = conn.createStatement();
			String sql = "select * from online_book where cate_id = '"+Long.valueOf(cate)+"' limit "+(now-1)+","+size+"";
			rs = st.executeQuery(sql);
			while(rs.next()){
				book = new Book();
				book.setBookId(rs.getLong(1));
				book.setAuthor(rs.getString(2));
				book.setBookName(rs.getString(3));
				book.setImgUrl(rs.getString(4));
				book.setIntroduce(rs.getString(5));
				book.setPrice(rs.getDouble(6));
				book.setPublish(rs.getString(7));
				book.setPublishDate(rs.getDate(8));
				book.setCategory(getCate(Long.valueOf(cate)));
				
				books.add(book);
				/*paging.setBooks(books);
				paging.setPageNow(now);
				paging.setPageSize(size);
				
				int rowCount = (int) this.getRowCount(cate);
				paging.setRowCount(rowCount);
				int pageCount = 0;
				
				if (rowCount % size == 0) {
					pageCount = rowCount / size;
				} else {
					pageCount = rowCount / size + 1;
				}
				
				paging.setPageCount(pageCount);*/
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JdbcUtils.closeResources(conn, st, rs);
		}
		return books;
		
	}

	@Override
	public Book selectBookById(String id) {
		
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		Book book = null;
		
		try {
			// 2.获取数据库连接
			conn = JdbcUtils.getConnection();
			// 3.创建语句对象
			st = conn.createStatement();
			String sql = "select * from online_book where bookid = '"+Long.valueOf(id)+"'";
			rs = st.executeQuery(sql);
			if(rs.next()){
				book = new Book();
				book.setBookId(rs.getLong(1));
				book.setAuthor(rs.getString(2));
				book.setBookName(rs.getString(3));
				book.setImgUrl(rs.getString(4));
				book.setIntroduce(rs.getString(5));
				book.setPrice(rs.getDouble(6));
				book.setPublish(rs.getString(7));
				book.setPublishDate(rs.getDate(8));
				book.setCategory(getCate(Long.valueOf(rs.getLong(9))));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JdbcUtils.closeResources(conn, st, rs);
		}
		return book;
		
	}
	@Override
	public long getRowCount(String cateId) {
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		long rowCount = 0;
		
		try {
			conn = JdbcUtils.getConnection();
			st = conn.createStatement();
			String sql = "select count(*) from online_book where cate_id = '"+Long.valueOf(cateId)+"'";
			rs = st.executeQuery(sql);
			if(rs.next()){
				rowCount = rs.getLong(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JdbcUtils.closeResources(conn, st, rs);
		}
		return rowCount;
	}
	@Override
	public Category getCate(Long id) {
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		Category cate = null;
		
		try {
			conn = JdbcUtils.getConnection();
			st = conn.createStatement();
			String sql = "select * from online_category where cateid = '"+id+"'";
			rs = st.executeQuery(sql);
			if(rs.next()){
				cate = new Category();
				cate.setCateId(rs.getLong(1));
				cate.setCateName(rs.getString(2));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JdbcUtils.closeResources(conn, st, rs);
		}
		return cate;
	}
	

}
